package com.google.project.client.Director;

import java.util.List;

import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.MultiSelectionModel;

import com.google.project.client.ProjectManager.PMEntrypoint;
import com.google.project.client.Service.ProjectService;
import com.google.project.client.Service.ProjectServiceAsync;
import com.google.project.client.Service.SubConstructorService;
import com.google.project.client.Service.SubConstructorServiceAsync;
import com.google.project.shared.ProjectDTO;
import com.google.project.shared.SubConstructorDTO;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.event.dom.client.ClickEvent;

public class DRTSubConstructor extends Composite {

	private static final Binder binder = GWT.create(Binder.class);

	interface Binder extends UiBinder<Widget, DRTSubConstructor> {
	}
	@UiField(provided=true) CellTable<SubConstructorDTO> ctbSubConstructor = new CellTable<SubConstructorDTO>();
	@UiField SimplePager pager;
	@UiField Button btnBack;
	@UiField Label lblProject;

	private final SubConstructorServiceAsync subConstructorService = GWT.create(SubConstructorService.class);
    private String projectId;
    ProjectDTO currProject = new ProjectDTO();
	final MultiSelectionModel<SubConstructorDTO> selectionModel = new MultiSelectionModel<SubConstructorDTO>();
	private final ProjectServiceAsync projectService = GWT.create(ProjectService.class);
	
	
	public DRTSubConstructor() {
		initTable();
		bindData();
		initWidget(binder.createAndBindUi(this));
	}
	
	public DRTSubConstructor(ProjectDTO project) {
		initWidget(binder.createAndBindUi(this));
		currProject=project;
		this.projectId=project.getProjectID();
	
			String projectName = project.getProjectName();
			
			lblProject.setText("Dự án : "+projectName);
		
		initTable();
		bindData();
	
		
	}
	
	public void bindData() {

		projectService.getListSubContructor(projectId, new AsyncCallback<List<SubConstructorDTO>>() {
			
			@Override
			public void onSuccess(List<SubConstructorDTO> result) {
				// TODO Auto-generated method stub
				LoadData(result);
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
	
	}
	
	//Init Cell Table
	public void initTable() {
		TextColumn<SubConstructorDTO> index = new TextColumn<SubConstructorDTO>() {
			@Override
			public String getValue(SubConstructorDTO subConstructor) {
				return subConstructor.getIndex()+"";
			}
		};
		ctbSubConstructor.setSelectionModel(selectionModel,
		    DefaultSelectionEventManager.<SubConstructorDTO> createCheckboxManager());
		Column<SubConstructorDTO, Boolean> checkColumn = new Column<SubConstructorDTO, Boolean>(
		    new CheckboxCell(true, false)) {
		  @Override
		  public Boolean getValue(SubConstructorDTO object) {
		    // Get the value from the selection model.
		    return selectionModel.isSelected(object);
		  }
		};
		
		
		

		TextColumn<SubConstructorDTO> subConstructorNameColumn = new TextColumn<SubConstructorDTO>() {
			@Override
			public String getValue(SubConstructorDTO subConstructor) {
				return subConstructor.getConstructorName();
			}
		};
		TextColumn<SubConstructorDTO> description = new TextColumn<SubConstructorDTO>() {
			@Override
			public String getValue(SubConstructorDTO subConstructor) {
				return subConstructor.getDescription();
			}
		};
		TextColumn<SubConstructorDTO> consultantName = new TextColumn<SubConstructorDTO>() {
			@Override
			public String getValue(SubConstructorDTO subConstructor) {
				return subConstructor.getConsultantName();
			}
		};
		TextColumn<SubConstructorDTO> phone = new TextColumn<SubConstructorDTO>() {
			@Override
			public String getValue(SubConstructorDTO subConstructor) {
				return subConstructor.getPhone();
			}
		};
		
		ctbSubConstructor.setStyleName("ttable");
		ctbSubConstructor.setWidth("100%", true);
		
		ctbSubConstructor.addColumn(index, "STT");
		ctbSubConstructor.setColumnWidth(index, 5.0, Unit.PCT);
		//ctbSubConstructor.addColumn(checkColumn, SafeHtmlUtils.fromSafeConstant("<br/>"));
		//ctbSubConstructor.setColumnWidth(checkColumn, 40, Unit.PX);
		ctbSubConstructor.addColumn(subConstructorNameColumn, "Tên nhà thầu phụ");
		ctbSubConstructor.setColumnWidth(subConstructorNameColumn, 30.0, Unit.PCT);
		ctbSubConstructor.addColumn(description, "Mô tả");
		ctbSubConstructor.setColumnWidth(description, 30.0, Unit.PCT);
		ctbSubConstructor.addColumn(consultantName, "Người đại diện");
		ctbSubConstructor.setColumnWidth(consultantName, 20.0, Unit.PCT);
		ctbSubConstructor.addColumn(phone, "Điện thoại");
		ctbSubConstructor.setColumnWidth(phone, 15.0, Unit.PCT);
	}
	
	/*
	 * @param subConstructorList
	 */
	public void LoadData(List<SubConstructorDTO> subConstructorList) {
		
		ListDataProvider<SubConstructorDTO> dataProvider = new ListDataProvider<SubConstructorDTO>();
		
		// Connect the table to the data provider.
		

		// Add the data to the data provider, which automatically pushes it to
		// the widget
		
		List<SubConstructorDTO> list = dataProvider.getList();
		int index=1;
		for (SubConstructorDTO subConstructor : subConstructorList) {
			subConstructor.setIndex(index);
			list.add(subConstructor);
			index++;
		}
		pager.setDisplay(ctbSubConstructor);
		pager.setPageSize(5);
		dataProvider.addDataDisplay(ctbSubConstructor);
	}
		
	@UiHandler("btnBack")
	void onBtnBackClick(ClickEvent event) {
		DRTEntrypoint.get().DRTProjectDetail(currProject.getProjectID());
	}
}
